package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.pojo.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.common.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理")
public class DishController {
    @Autowired
    private DishService dishService;
    @RequestMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        return dishService.page(dishPageQueryDTO);
    }
    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> add(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        boolean result = dishService.add(dishDTO);
        return result ? Result.success("菜品添加成功") : Result.error("新增菜品失败");
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> findById(@PathVariable Long id) {
        return dishService.findById(id);
    }
    @DeleteMapping
    @ApiOperation("根据id删除菜品")
    public Result<String> deleteByIds (@RequestParam List<Long> ids) {
        return dishService.deleteByIds(ids) ? Result.success("删除成功") : Result.error("删除失败");
    }
    @PutMapping
    @ApiOperation("修改菜品")
    public Result<String> update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}", dishDTO);
        return dishService.update(dishDTO) ? Result.success("修改成功") : Result.error("修改失败");
    }
    @PostMapping("/status/{status}")
    @ApiOperation("起售停售菜品")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        return dishService.startOrStop(status, id) ? Result.success("状态修改成功") : Result.error("状态修改失败");
    }
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Integer categoryId) {
        return Result.success(dishService.list(categoryId));
    }
}
